package me.zhengjie.modules.workorder.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.workorder.domain.TCustomerSuggest;
import me.zhengjie.modules.workorder.service.TCustomerSuggestService;
import me.zhengjie.modules.workorder.service.dto.TCustomerSuggestQueryCriteria;
import me.zhengjie.utils.HttpResponseUtils;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;


/**
* @author zcw
* @date 2019-11-10
*/
@RestController
@RequestMapping("api")
public class TCustomerSuggestController {

    @Autowired
    private TCustomerSuggestService tCustomerSuggestService;

    @Log("查询TCustomerSuggest")
    @GetMapping(value = "/tCustomerSuggest")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERSUGGEST_ALL','TCUSTOMERSUGGEST_SELECT')")
    public ResponseEntity getTCustomerSuggests(TCustomerSuggestQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(tCustomerSuggestService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增TCustomerSuggest")
    @PostMapping(value = "/tCustomerSuggest")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERSUGGEST_ALL','TCUSTOMERSUGGEST_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TCustomerSuggest resources){
        //回访操作,只取到场标志及到场时间
        resources.setRevisitorTime(new Timestamp(System.currentTimeMillis()));
        resources.setRevisitor(SecurityUtils.getName());
        return new ResponseEntity(tCustomerSuggestService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改TCustomerSuggest")
    @PutMapping(value = "/tCustomerSuggest")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERSUGGEST_ALL','TCUSTOMERSUGGEST_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TCustomerSuggest resources){
        try {
            //回访操作,只取到场标志及到场时间
            resources.setRevisitorTime(new Timestamp(System.currentTimeMillis()));
            resources.setRevisitor(SecurityUtils.getUsername());
            tCustomerSuggestService.update(resources);
            return new ResponseEntity(HttpStatus.OK);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }


    @Log("评价TCustomerSuggest")
    @PutMapping(value = "/tCustomerSuggest/evaluate")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERSUGGEST_ALL','TCUSTOMERSUGGEST_EDIT')")
    public ResponseEntity evaluate(@Validated @RequestBody TCustomerSuggest resources){
        try {
            //回访操作,只取到场标志及到场时间
            //resources.setRevisitorTime(new Timestamp(System.currentTimeMillis()));
            //resources.setRevisitor(SecurityUtils.getName());
            resources.setInputOperator(SecurityUtils.getName());
            resources.setEvaluator(SecurityUtils.getName());
            resources.setEvaluatorTime(new Timestamp(System.currentTimeMillis()));
            tCustomerSuggestService.evaluate(resources);
            return new ResponseEntity(HttpStatus.OK);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }

    @Log("批量评价TCustomerSuggest")
    @PutMapping(value = "/tCustomerSuggest/batchEvaluate")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERSUGGEST_ALL','TCUSTOMERSUGGEST_EDIT')")
    public ResponseEntity batchEvaluate(@Validated @RequestBody TCustomerSuggest resources){
        try {
            //回访操作,只取到场标志及到场时间
            //resources.setRevisitorTime(new Timestamp(System.currentTimeMillis()));
            //resources.setRevisitor(SecurityUtils.getName());
            resources.setInputOperator(SecurityUtils.getName());
            resources.setEvaluator(SecurityUtils.getName());
            resources.setEvaluatorTime(new Timestamp(System.currentTimeMillis()));
            tCustomerSuggestService.batchEvaluate(resources);
            return new ResponseEntity(HttpStatus.OK);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }

    @Log("删除TCustomerSuggest")
    @DeleteMapping(value = "/tCustomerSuggest/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERSUGGEST_ALL','TCUSTOMERSUGGEST_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        tCustomerSuggestService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}